import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { stringFormat } from '/@/utils/common/compUtils';

enum Api {
  list = '/api-upms/sys/calendar/task',
  save = '/api-upms/sys/calendar/saveOrUpdate',
  edit = '/api-upms/sys/calendar/saveOrUpdate',
  get = '/api-upms/sys/calendar/queryById',
  checked = '/api-upms/sys/calendar/checked',
  delete = '/api-upms/sys/calendar/delete',
  deleteBatch = '/api-upms/sys/calendar/deleteBatch',
  getCurrentUserTenants = '/api-upms/sys/calendar/queryCurrentUserTenants',
}

/**
 * 查询日程列表
 * @param params
 */
export const getTaskList = (params) => {
  return defHttp.get({ url: Api.list, params });
};

/**
 * 保存或者更新日程
 * @param params
 */
export const saveOrUpdateTask = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/* 更新日程为完成
 * @param params
 */
export const checkedTask = (params, handleSuccess) => {
  Modal.confirm({
    title: '提示',
    content: '确认该事项标记为已完成？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.post({ url: Api.checked, params }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 查询日程详情
 * @param params
 */
export const getTaskById = (params) => {
  return defHttp.get({ url: Api.get, params });
};

/**
 * 删除日程
 * @param params
 */
export const deleteTask = (params, handleSuccess) => {
  Modal.confirm({
    title: '提示',
    content: '确认删除该事项？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.delete, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 批量删除日程
 * @param params
 */
export const batchDeleteTask = (params, handleSuccess) => {
  Modal.confirm({
    title: '提示',
    content: '是否删除选中数据？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};
